package jtk.project4.fleet.task;

import java.util.List;


import jtk.project4.fleet.domain.RbDept;
import jtk.project4.fleet.event.WorkOrderManagementEvent;
import jtk.project4.fleet.event.WorkOrderManagementEvent.WorkOrderManagementEventType;
import jtk.project4.fleet.service.DatabaseService;
import nl.coderight.jazz.Task;
import nl.coderight.jazz.event.MessageEvent;
import nl.coderight.jazz.event.ProgressEvent;
import nl.coderight.jazz.event.MessageEvent.MessageEventType;
import nl.coderight.jazz.event.ProgressEvent.ProgressEventType;

public class LoadRbDeptTask extends Task<List<RbDept>> {
	
	@Override
	public List<RbDept> process() throws Exception {
		// TODO Auto-generated method stub
		return DatabaseService.getInstance().getRbDept();
	}

	protected void onStart() {
		ProgressEvent progressEvent = new ProgressEvent(ProgressEventType.START);
		progressEvent.setMessage("Loading contacts...");
		progressEvent.setIndeterminate(true);
		postEvent(progressEvent);
	}
	
	@Override
	protected void onFinish() {
		postEvent(new ProgressEvent(ProgressEventType.STOP));
		
		WorkOrderManagementEvent rbDeptEvent = new WorkOrderManagementEvent(WorkOrderManagementEventType.LOAD_SUCCESS);
		rbDeptEvent.setRbDept(getResult());
		postEvent(rbDeptEvent);
	}
	
	@Override
	protected void onError() {
		MessageEvent error = new MessageEvent(MessageEventType.ERROR);
		error.setMessage(getError().getMessage());
		postEvent(error);			
	}

}
